package site.lyweb.service.impl;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import site.lyweb.constant.Constant;
import site.lyweb.domain.dto.*;
import site.lyweb.domain.query.StudentCourseQuery;
import site.lyweb.mapper.StudentCourseMapper;
import site.lyweb.service.StudentCourseService;
import site.lyweb.utils.ExtBeanUtils;
import site.lyweb.utils.UUIDUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 深山老林
 * @description 针对表【student】的数据库操作Service实现
 * @createDate 2024-06-19 20:53:32
 */
@Service
public class StudentCourseServiceImpl implements StudentCourseService {
    @Resource
    private StudentCourseMapper studentCourseMapper;
    /**
     * 分页 + 搜索
     *
     * @param current
     * @param studentCourseQuery
     * @return
     */
    @Override
    public PageDTO<StudentCourseDTO> getStudentCourseByPage(Integer current, StudentCourseQuery studentCourseQuery) {
        //分页查询分两个查询，1、查符合条件的数据总条数；2、查询当前页的数据
        int totalRow = studentCourseMapper.selectByCount(studentCourseQuery);
        int start = (current - 1) * Constant.PAGE_SIZE;
        List<StudentCourse> studentCoursesList = studentCourseMapper.selectByPage(studentCourseQuery);
        int totalPage = totalRow / Constant.PAGE_SIZE;
        if (totalRow % Constant.PAGE_SIZE > 0) { //如果除不尽，总页数就需要加1
            totalPage ++;
        }
        //需要把 List<TUser> 转换为 List<UserInfoDTO>
        List<StudentCourseDTO> studentCoursesDTOList = ExtBeanUtils.copyPropertiesForList(studentCoursesList, StudentCourseDTO.class);

        return new PageDTO<>(current, Constant.PAGE_SIZE, totalRow, totalPage, studentCoursesDTOList);

    }

    @Override
    public boolean removeBatchByIds(List<String> idList) {
        try {
            return studentCourseMapper.removeBatchByIds(idList);
        } catch (Exception e) {
            // 可以根据具体情况处理异常，比如记录日志或者抛出自定义异常
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean removeById(String id) {
        try {
            return studentCourseMapper.removeById(id);
        } catch (Exception e) {
            // 可以根据具体情况处理异常，比如记录日志或者抛出自定义异常
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean save(String id, String studentId, String courseId,String teacherId, BigDecimal courseScore) {
        try {
            //无id：插入方法
            if(id==null || id.isEmpty()){
                id = UUIDUtils.getUUID();
                return studentCourseMapper.insert(id, studentId, courseId,teacherId,courseScore);
            } else {
                return studentCourseMapper.update(id, studentId, courseId,teacherId,courseScore);
            }
        } catch (Exception e) {
            // 可以根据具体情况处理异常，比如记录日志或者抛出自定义异常
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public StuCourseRelation getById(String id) {
        try {
            return studentCourseMapper.getById(id);
        } catch (Exception e) {
            // 可以根据具体情况处理异常，比如记录日志或者抛出自定义异常
            e.printStackTrace();
            return null;
        }
    }

}




